<template>
    <div class="app-container">
      <header-title/>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="海报名称" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入海报名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="tableList">
        <el-table-column label="海报ID" align="center" prop="id" width="100"/>
        <el-table-column label="海报名称" align="center" prop="title"/>
        <el-table-column label="海报图片" align="center">
          <template slot-scope="scope">
            <el-image v-if="scope.row.url" fit="cover" :style="{width: scope.row.width/10+'px',height: scope.row.height/10+'px'}" :src="scope.row.url" :preview-src-list="[scope.row.url]" :initial-index="0"></el-image>
            <div v-else>未上传</div>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >更新图片</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="海报图片" prop="url">
            <el-upload
              :action="uploadFileUrl"
              :headers="headers"
              :on-success="handleUploadSuccess"
              :show-file-list="false"
              class="poster-uploader">
              <img v-if="form.url" :src="form.url" class="poster" :style="{width: form.width/2+'px',height: form.height/2+'px'}">
              <i v-else class="el-icon-plus poster-uploader-icon" :style="{width: form.width/2+'px',height: form.height/2+'px','line-height': form.height/2+'px'}"></i>
              <div slot="tip" class="color-red">建议上传尺寸：{{form.width}}px * {{form.height}}px</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listPosterImage, addPosterImage } from "@/api/operateManage/posterImage";
  import { getToken } from "@/utils/auth";
  export default {
    name: "PosterImage",
    data() {
      return {
        uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        tableList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          keyword: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          url: [
            { required: true, message: "海报图片不能为空", trigger: "change" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询参数列表 */
      getList() {
        this.loading = true;
        listPosterImage(this.queryParams).then(response => {
            this.tableList = response.data.records;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          name: undefined,
          sortNum: 0
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = JSON.parse(JSON.stringify(row));
        this.open = true;
        this.title = "更新"+row.title;
      },
      handleUploadSuccess(response, file, fileList) {
        this.form.url = response.fileUrl
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addPosterImage(this.form).then(response => {
              this.$modal.msgSuccess("更新海报图片成功");
              this.open = false;
              this.getList();
            });
          }
        });
      }
    }
  };
  </script>
  <style lang="scss">
  .poster-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover:after{
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: rgba(0,0,0,0.5);
      color: #fff;
      font-size: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: "element-icons" !important;
    }
  }
  
  .poster-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  
  .poster-uploader-icon {
    font-size: 60px;
    color: #8c939d;
    text-align: center;
  }
  
  .poster {
    display: block;
  }
  </style>
  